package javango.http;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javango.contrib.sessions.Session;
import javango.core.AnonymousUser;
import javango.core.User;


public class HttpRequest {

	public interface Listener {
		public void closeRequest(HttpRequest request);
	}
	
	protected String path;
	protected String context;
	protected User user;
	protected Map<String, String[]> parameterMap;
	protected String method;
	protected Session session;
	protected List<Listener> listeners = new ArrayList<Listener>();

	/**
	 * Helper function to get the first value for a parameter
	 * 
	 * @param key
	 * @return
	 */
	public String getParameter(String key) {
		String[] v = parameterMap.get(key);
		if (v == null) return null;
		if (v.length == 0) return null;
		return v[0];
	}
	
	public void addListener(Listener listener) {
		listeners.add(listener);
	}
	public void close() {
		for (Listener l : listeners) {
			l.closeRequest(this);
		}
	}
	
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

	public User getUser() {
		if (user == null) {
			user = new AnonymousUser();
		}
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Map<String, String[]> getParameterMap() {
		return parameterMap;
	}

	public void setParameterMap(Map<String, String[]> parameterMap) {
		this.parameterMap = parameterMap;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}
	

}
